<template>
	<div>
		<v-file-input v-if="UpLoad.isUploadMultipleFiles" multiple @change="upLoadFiles" v-model="fileInfo"
			color="deep-purple accent-4" counter label="上传文件" placeholder="请选择需要上传的文件" prepend-icon="mdi-paperclip"
			outlined :show-size="1000">
			<template v-slot:selection="{ index, text }">
				<v-chip v-if="index < 2" color="deep-purple accent-4" dark label small>
					{{ text }}
				</v-chip>

				<span v-else-if="index === 2" class="text-overline grey--text text--darken-3 mx-2">
					+{{ fileInfo.length - 2 }} File(s)
				</span>
			</template>
		</v-file-input>
		<v-file-input v-if="UpLoad.isUploadSingleImage" @change="upLoadImage" v-model="fileInfo"
			color="deep-purple accent-4" counter label="上传文件" placeholder="请选择需要上传的文件" prepend-icon="mdi-paperclip"
			outlined :show-size="1000">
			<template v-slot:selection="{ index, text }">
				<v-chip v-if="index < 2" color="deep-purple accent-4" dark label small>
					{{ text }}
				</v-chip>

				<span v-else-if="index === 2" class="text-overline grey--text text--darken-3 mx-2">
					+{{ fileInfo.length - 2 }} File(s)
				</span>
			</template>
		</v-file-input>
	</div>
</template>

<script>
	import cookies from '@/utils/util.cookies.js';
	export default {
		name: 'uploadfile',
		props: {
			callBackPath: {
				type: Function, //参数类型：函数
			},
			UpLoad: {
				isUploadSingleImage: false,
				isUploadMultipleFiles: false,
				fileUrl: `Common/UploadFiles?`,
				type: 0
			}
		},
		data() {
			return {
				fileInfo: null
			}
		},
		methods: {
			// 上传单个图片
			async upLoadImage(file) {
				var formData = new window.FormData();
				formData.append('max_ver', file);
				var response = await this.$httpRequest({
					url: `Common/UploadImage?type=${this.UpLoad.type}`,
					method: 'post',
					data: formData,
					headers: {
						'Content-Type': 'multipart/form-data'
					}
				})
				if (response.status == 1) {
					this.$props.callBackPath(response.data.link)
				} else {
					this.$message.error(response.info)
				}
			},
			//上传多文件
			async upLoadFiles() {
				if (this.fileInfo.length <= 0) {
					return
				}
				var formData = new window.FormData();
				for (let file of this.fileInfo) {
					formData.append("files", file, file.name);
				}
				var token = cookies.get('token');
				if(this.UpLoad.fileUrl==null || this.UpLoad.fileUrl==undefined){
					this.UpLoad.fileUrl=`Common/UploadFiles?`
				}
				var response = await this.$httpRequest({
					url: `${this.UpLoad.fileUrl}type=${this.UpLoad.type}`,
					method: 'post',
					data: formData,
					headers: {
						'Content-Type': 'multipart/form-data',
						Authorization: token,
					}
				})
				if (response.status == 1) {
					this.$props.callBackPath(response.data)
				} else {
					this.$message.error(response.info)
				}
			},
		}
	}
</script>

<style>
</style>
